package com.phuc.game.tankshooter.engine;

import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.util.MathUtils;

import com.phuc.game.tankshooter.MainActivity;
import com.phuc.game.tankshooter.sprite.*;

public class EnemyFactory {

	public static final int ENEMY_TANK_A_RED = 6001;
	public static final int ENEMY_TANK_A_GREEN = 6002;
	public static final int ENEMY_TANK_A_GOLD = 6008;
	public static final int ENEMY_TANK_TIGER_RED = 6003;
	public static final int ENEMY_TANK_TIGER_GREEN = 6009;
	public static final int ENEMY_TANK_TIGER_GOLD = 6010;
	public static final int ENEMY_TANK_MOLE_ORANGE = 6004;
	public static final int ENEMY_TANK_MOLE_PUPLE = 6011;
	public static final int ENEMY_TANK_T34_ORANGE = 6005;
	public static final int ENEMY_TANK_T34_GREEN = 6012;
	public static final int ENEMY_TANK_T34_GOLD = 6013;
	public static final int ENEMY_TRUCK_ORANGE = 6006;
	public static final int ENEMY_FUEL_TRUCK_RED = 6007;
	
	public static void createEnemy(int enemyCode, Path[] path) {
		Scene welcomeScene = MainActivity.getInstant().getGamePlay().getScene();
		EnemyObjectSprite faceSpr;
		
		switch (enemyCode) {
		case ENEMY_TANK_A_RED:
			faceSpr = new EnemyTankARed(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_A_GREEN:
			faceSpr = new EnemyTankAGreen(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_A_GOLD:
			faceSpr = new EnemyTankAGold(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_TIGER_RED:
			faceSpr = new EnemyTankTigerRed(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_TIGER_GREEN:
			faceSpr = new EnemyTankTigerGreen(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_TIGER_GOLD:
			faceSpr = new EnemyTankTigerGold(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_MOLE_ORANGE:
			faceSpr = new EnemyTankMoleOrange(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_MOLE_PUPLE:
			faceSpr = new EnemyTankMolePuple(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_T34_ORANGE:
			faceSpr = new EnemyTankT34Orange(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_T34_GREEN:
			faceSpr = new EnemyTankT34Green(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TANK_T34_GOLD:
			faceSpr = new EnemyTankT34Gold(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_TRUCK_ORANGE:
			faceSpr = new EnemyTruck(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		case ENEMY_FUEL_TRUCK_RED:
			faceSpr = new EnemyFuelTruck(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		default:
			faceSpr = new EnemyTankARed(welcomeScene, path);
			welcomeScene.attachChild(faceSpr);
			break;
		}
	}
	
	/**
	 * Tao 1 nhom ke thu, bao gom toan nhung thang giong nhau
	 * @param enemyCode enemy code.
	 * @param rowNum number of enemy in each row
	 * @param colNum number of enemy in each column
	 * @param range range between objects
	 * @param pathType
	 */
	public static void createGroupOneType(int enemyCode,
			int rowNum, int colNum, int range, int pathType) {
		// Khoi tao path cho thang enemy vi tri bottom left
		int randNum = MathUtils.random(0, 1);
		boolean startAtLeft = randNum == 0;
		
		Path pathTmp;
		int x1;
		int y1;
		int x2;
		int y2;
		int groupWidth = range * (colNum - 1);
		
		switch (pathType) {
		case PathType.STRAIGHT: {
			x1 = MathUtils.random(0, MainActivity.SCENE_WIDTH
					- groupWidth - MovingPathFactory.SIZE_OBJECT_WIDTH);
			x2 = x1;
			y1 = -MovingPathFactory.SIZE_OBJECT_HEIGHT;
			y2 = MainActivity.SCENE_END_LOCATION;
			
			int xTmp1;
			int yTmp1;
			int xTmp2;
			int yTmp2 = y2; // Khong thay doi voi tat ca doi tuong trong nhom
			
			// Row dem nguoc tu duoi len
			for (int row = 0; row < rowNum; row++) {
				yTmp1 = y1 - range * row;
				for (int col = 0; col < colNum; col++) {
					xTmp1 = x1 + range * col;
					xTmp2 = xTmp1;
					pathTmp = MovingPathFactory.createLinePath(
							xTmp1, yTmp1, xTmp2, yTmp2);
					createEnemy(enemyCode, new Path[] {pathTmp});
				}
			}

			break;
		}
		case PathType.LINE: {
			x1 = MathUtils.random(0, MainActivity.SCENE_WIDTH
					- groupWidth - MovingPathFactory.SIZE_OBJECT_WIDTH);
			x2 = MathUtils.random(0, MainActivity.SCENE_WIDTH
					- groupWidth - MovingPathFactory.SIZE_OBJECT_WIDTH);
			y1 = -MovingPathFactory.SIZE_OBJECT_HEIGHT;
			y2 = MainActivity.SCENE_END_LOCATION;
			
			int xTmp1;
			int yTmp1;
			int xTmp2;
			int yTmp2 = y2; // Khong thay doi voi tat ca doi tuong trong nhom
			
			// Row dem nguoc tu duoi len
			for (int row = 0; row < rowNum; row++) {
				yTmp1 = y1 - range * row;
				for (int col = 0; col < colNum; col++) {
					xTmp1 = x1 + range * col;
					xTmp2 = x2 + range * col;
					pathTmp = MovingPathFactory.createLinePath(
							xTmp1, yTmp1, xTmp2, yTmp2);
					createEnemy(enemyCode, new Path[] {pathTmp});
				}
			}
			break;
		}
		case PathType.ZIGZAG_3:
			
			break;
		default:
			break;
		}
		
	}
	
	public static interface PathType {
		public static final int ZIGZAG_3 = 1;
		public static final int STRAIGHT = 2;
		public static final int LINE = 3;
	}
	
	public static int getRangeOfTankGroup(int tankId) {
		int range = 0;
		switch (tankId) {
		case ENEMY_TANK_TIGER_RED:
		case ENEMY_TANK_TIGER_GREEN:
			range = 85;
			break;
		case ENEMY_TANK_A_RED:
		case ENEMY_TANK_A_GREEN:
			range = 65;
			break;
		case ENEMY_TANK_T34_GREEN:
		case ENEMY_TANK_T34_ORANGE:
			range = 90;
			break;
		case ENEMY_TANK_MOLE_ORANGE:
		case ENEMY_TANK_MOLE_PUPLE:
			range = 70;
			break;
		default:
			range = 65;
			break;
		}
		
		return range;
	}
}
